package com.tsing.common.Demo;

import com.tsing.common.mapper.LogMapper;
import com.tsing.common.utils.BeanCoperUtil;
import com.tsing.common.utils.DateTimeUtil;
import com.tsing.common.utils.ExcelUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author TheTsing
 * @version 1.0
 * @date 2021/4/29 10:25
 */
//@RestController
public class TestExcelUtil {

    @Resource
    private LogMapper logMapper;

    @GetMapping("/test")
    public void test(HttpServletResponse response) throws Exception {
        List<LogExcelVo> logExcelVos = BeanCoperUtil.copyObjects(logMapper.selectList(null), LogExcelVo.class);
        String fileName = DateTimeUtil.getStrByLocalDateTimeAndFormat(DateTimeUtil.getLocalDateTimeNow(), "yyyyMMddHHmmss") + "_日志记录表";
        try {
            ExcelUtil.exportExcel(logExcelVos, "日志记录", "日志记录表", LogExcelVo.class, fileName, true, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/test2")
    public void test(@RequestPart("file") MultipartFile file) throws Exception {
        List<LogExcelVo> logExcelVos = ExcelUtil.importExcel(file, 1, 1, true, LogExcelVo.class);
        logExcelVos.forEach(System.out::println);
    }

}
